﻿using Ross.EntityFramework;
using Ross.Service;
using Ross.Service.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Http.Controllers;

namespace Ross.Traning.Controllers
{
    [SimpleAuthenticationFilter]
    public class UsersController : BaseApiController
    {        
        [HttpGet]
        public JsonResult<ResultDto<Users>> Get()
        {
            ResultDto<Users> result = new ResultDto<Users>();
            using (UserService serv = new UserService(dbContextStr))
            {            
                result = serv.Get();
            }
            return Json(result);
        }
        [HttpGet]
        public JsonResult<ResultDto<List<Users>>> GetPageList(string username = "", string empid = "",string role = "", int pageindex = 1, int pagesize = 10)
        {
            ResultDto<List<Users>> result = new ResultDto<List<Users>>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.GetPageList(pageindex, pagesize, username, empid, role);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> Insert([FromBody] Users vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.Create(vm);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> Update([FromBody] Users vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.Update(vm);
            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> UpdatePoint([FromBody] Users vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.UpdatePoint(vm);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> ChangePsw([FromBody] UptPsw vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.UpdatePsw(vm);
            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> ResetPsw(long userId,string password)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.ResetPsw(userId, password);
            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> Delete(long id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.Delete(id);
            }
            return Json(result);
        }
        [HttpGet]
        public JsonResult<ResultDto<List<Users>>> GetList()
        {
            ResultDto<List<Users>> result = new ResultDto<List<Users>>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.GetAllList();
            }
            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<Users>> GetByID(long id)
        {
            ResultDto<Users> result = new ResultDto<Users>();
            using (UserService serv = new UserService(dbContextStr))
            {
                result = serv.GetByID(id);
            }
            return Json(result);
        }
    }
}
